package com.mew.chess.core.game.qmcs.game.common;

import cn.hutool.core.util.RandomUtil;
import com.mew.common.util.GameUtil;
import com.zebra.common.ByteBufUtils;
import io.netty.buffer.ByteBuf;
import lombok.Data;
import lombok.ToString;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;

@Data
@ToString
public class QmcsSlots {

    public int type;
    public int num;
    public int mult;
    public long gold;
    public int[] slots;

    private static int[] typeWeights100 = {41, 120, 30, 100, 30, 50, 30, 100, 30, 120, 20, 128, 30, 20, 10, 30,111};
    private static int[] typeWeights150 = {150, 100, 110, 30, 120, 20, 40, 50, 20, 50, 20, 86, 50, 50, 50, 50, 4};
    private static int[][] types = {
            {19, 3,60},
            {19, 2,15},
            {18, 3,50},
            {18, 2,30},
            {16, 3,45},
            {16, 2,27},
            {17, 3,45},
            {17, 2,27},
            {14, 3,30},
            {14, 2,9},
            {15, 3,30},
            {15, 2,10},
            {1, 3,12},
            {1, 2,3},
            {2, 3,12},
            {2, 2,3},
            {-1, 0,0}
    };

    public void init(int bouns){

        int[] icons;
        if (bouns==0){
            icons = types[GameUtil.weights(typeWeights100)];
        }else {
            icons = types[GameUtil.weights(typeWeights150)];
        }

        type = icons[0];
        num = icons[1];
        mult = icons[2];

        HashSet<Integer> ic = new HashSet<>();

        while (ic.size() < 3-num){

            ic.add(types[RandomUtil.randomInt(types.length-1)][0]);
        }
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            list.add(type);
        }

        ic.stream().forEach(i->list.add(i));
        Collections.shuffle(list);
        slots = list.stream().mapToInt(Integer::intValue).toArray();
    }

    public void write(ByteBuf b){
        ByteBufUtils.writeInt(b,type);
        ByteBufUtils.writeLong(b,gold);
        ByteBufUtils.writeIntArray(b,slots);
    }

    public static void main(String[] args) {
        QmcsSlots qmcsSlots = new QmcsSlots();
        for (int i = 0; i < 50; i++) {
            qmcsSlots.init(0);
            System.out.println(qmcsSlots);
        }
    }
    
}
